package com.hacker.jms.topic;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * 消息提供者
 * @Author:这个程序员有纹身
 * @Date: Created in 14:35 2017\12\23 0023
 */
public class AppProducer {

    private static final String url = "tcp://127.0.0.1:61616";
    private static final String topicName = "topic-test";
    public static void main(String[] args) throws JMSException {

        //1、创建ConnectionFactory
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);

        //2、创建Connection
        Connection connection = connectionFactory.createConnection();

        //3、启动连接
        connection.start();

        //4、创建会话 是否使用事务  连接模式
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        //5、创建一个目标、主题
        Destination destination = session.createTopic(topicName);

        //6、创建生产者(地址)
        MessageProducer messageProducer = session.createProducer(destination);

        //7、循环向地址发送数据
        for(int i=0;i<100;i++){
            //8、创建消息
            TextMessage textMessage = session.createTextMessage("test"+i);
            //9、发布消息
            messageProducer.send(textMessage);

            System.out.println("发送消息"+textMessage.getText());
        }

        //10、关闭连接
        connection.close();
    }
}
